/*
 * Copyright (c) 2018-2999 深圳市阿毛科技有限公司 All rights reserved.
 *
 * https://www.lamyz.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.lam.mall.api.controller;


import java.util.Date;
import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lam.common.base.controller.BaseController;
import com.lam.common.base.domain.Result;
import com.lam.common.context.TokenUtils;
import com.lam.common.exception.DataNotExistException;
import com.lam.framework.operLog.annotation.Log;
import com.lam.mall.app.vo.ProductSimpleVo;
import com.lam.mall.entity.UserCollection;
import com.lam.mall.service.ProductService;
import com.lam.mall.service.UserCollectionService;
/**
 */
@RestController
@RequestMapping("/${frontPath}/user/collection")
@Log(title = "用户收藏")
public class UserCollectionApiController extends BaseController{

	@Autowired
    private UserCollectionService userCollectionService;
	@Autowired
    private ProductService productService;

//    @GetMapping("/page")
//    @Log(oper = "分页查询收藏数据")
//    public Result getUserCollection() {
//    	startPage();
//    	List<UserCollectionVo> list = userCollectionService.getPageByUserId(TokenUtils.getLoginUserid());
//        return Result.ok(getTableData(list));
//    }

    @GetMapping("isCollection")
    @Log(oper = "判断商品是否被收藏")
    public Result isCollection(String prodId) {
    	
        if (productService.getSampleProdByIdCache(prodId) == null) {
        	return Result.ok(false);
        }
        return Result.ok(userCollectionService.checkExist(TokenUtils.getLoginUserid(), prodId));
    }

    /**
     * 传入收藏商品id,如果商品未收藏则收藏商品，已收藏则取消收藏
     * @param prodId
     * @return
     */
    @PostMapping("/addOrCancel")
    @Log(oper = "添加/取消收藏")
    public Result addOrCancel(@RequestBody String prodId) {
        if (Objects.isNull(productService.getSampleProdByIdCache(prodId))) {
            throw new DataNotExistException("该商品不存在");
        }
        String userId = TokenUtils.getLoginUserid();
        if (userCollectionService.checkExist(userId, prodId)) {
            userCollectionService.delete(userId, prodId);
        } else {
            UserCollection userCollection = new UserCollection();
            userCollection.setCreateTime(new Date());
            userCollection.setUserId(userId);
            userCollection.setProdId(prodId);
            userCollectionService.insert(userCollection);
        }
        return Result.ok();
    }

    /**
     * 查询用户收藏商品数量
     */
    @GetMapping("count")
    @Log(oper = "用户收藏商品数量")
    public Result findUserCollectionCount() {
        String userId = TokenUtils.getLoginUserid();
        return Result.ok(userCollectionService.selectCount(userId));
    }

    @GetMapping("/prods")
    @Log(oper = "用户收藏商品列表")
    public Result collectionProds() {
        String userId = TokenUtils.getLoginUserid();
        startPage();
        List<ProductSimpleVo> list = productService.collectionProds(userId);
        return Result.ok(getTableData(list));
    }

}
